package com.yixincaipiao.ui.splash;

import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.yixincaipiao.R;
import com.yixincaipiao.base.BaseActivity;
import com.yixincaipiao.common.CommonConfig;
import com.yixincaipiao.exception.BaseException;
import com.yixincaipiao.network.Http;
import com.yixincaipiao.network.NetRequest;
import com.yixincaipiao.ui.account.Account;
import com.yixincaipiao.ui.account.LoginActivity;
import com.yixincaipiao.ui.account.modle.User;
import com.yixincaipiao.ui.main.MainActivity;
import com.yixincaipiao.utils.JsonUtil;
import com.yixincaipiao.utils.Strings;
import com.yixincaipiao.utils.UIUtils;
import com.yixincaipiao.weight.Dialogs;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/25.
 */

public class SplashActivity extends BaseActivity {//闪屏页===gif , 淡入淡出 , 等待
    private boolean isFirst = true;
    boolean infoException = false;  //保存的信息异常

    @Override
    protected void initView() {
        setContentView(R.layout.splash_layout);

    }

    @Override
    protected void onResume() {//判断第一次启动,引导页? 登录页? 主页
        super.onResume();
        getData();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isFirst) {
                    //
                    SharedPreferences.Editor editer = CommonConfig.instance().editor.putBoolean("isFirset", false);
                    editer.commit();
                    WelcomeActivity.actionView(SplashActivity.this, 0);
                    finish();
                } else {
                    //main or login


                    User user = Account.Instance(getApplicationContext()).getUser();
                    //判断用户数据 是否合法,是登陆后进主页 否 登陆页面
                    if (user != null && Strings.isNotEmpty(user.getLogin_id())) {
                        String url = "http://www.dyxstar.com/app/yxq/Api/Login/login";
                        Map<String, Object> params = new HashMap<>();
                        params.put("mobile", user.getMobile_login());
                        params.put("passwd", "d05faede6822635cb1559ff04592bb05a");//暂行
                        Http.aSyncPost(getApplicationContext(), Dialogs.getWaitDialog(SplashActivity.this), url, params, new NetRequest.NetRequestListener() {
                            @Override
                            public void onAccessComplete(boolean success, String object, VolleyError error, String flag) {

                                try {
                                    String resultData = Http._analysData(object);
                                    if (resultData == null) {
                                        //弹出原因
//                                        infoException = true;
                                        LoginActivity.actionView(SplashActivity.this, 0);
                                        finish();
                                        return;
                                    }
                                    //存到spzhong去
                                    Log.i("after", "返回数据               " + resultData);
                                    MainActivity.actionView(SplashActivity.this, 0);
                                    finish();
                                } catch (BaseException e) {
                                    Account.Instance(getApplicationContext()).logout();
                                    UIUtils.showToastShort(SplashActivity.this, "" + e.getMessage());
                                    LoginActivity.actionView(SplashActivity.this, 0);
                                    finish();
                                }
                            }
                        });

                    } else {
                        LoginActivity.actionView(SplashActivity.this, 0);
                        finish();
                    }


                }
            }
        }, 1500);

    }

    @Override
    protected void getData() {
        isFirst = CommonConfig.instance().sharedPreferences.getBoolean("isFirset", true);
    }
}
